// package/pages/payment/payment.js
const utils = require('../../../utils/util.js');
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxbarcode = require('../../../utils/wxbarcode/index.js')
const app = getApp();
var timeOut = null //倒计时函数
Page({
  data: {
    code: "",
    num: 1
  },


  getData(e) {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getCardNumberCode, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        var data = res.data.result.card_number_code;
        setTimeout(() => {
          wxbarcode.barcode("barcode", data, 680, 150);
          wxbarcode.qrcode("qrcode", data, {
            codeSize: 480,
            color: "#000000",
            bgcolor: "#ffffff",
          });
          that.setData({
            code: data
          })
          that.checkCoupon(res.data.result.card_number_code)

        }, 500)
      } else {

        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            //返回tab页面刷新数据
            wx.setStorageSync('refresh', true)
            //返回优惠券页面刷新数据
            wx.setStorageSync('refreshCoupon', true)
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    }).catch(err => {
      
    
    })
  },

  checkCoupon(code) {
    const that = this;
    let data = {
      code,
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.checkupNotificationNew, data).then(res => {
      // console.log(res, '扫描支付码返回的状态')
      if (res.data.code == 200) {
        if (Array.isArray(res.data.data.info) || res.data.data.info.status != 1) {
          timeOut = setTimeout(() => {
            let num = that.data.num;
            that.setData({
              num: num + 1
            })
            console.log(num, 9999)
            that.checkCoupon(code)
          }, 2000)
        } else {
          clearTimeout(timeOut)
          let memberCardCode = res.data.data.info.information.memberCardCode
          let saleMode = res.data.data.info.information.saleMode
          let orgname = res.data.data.info.information.orgname
          let money = res.data.data.info.information.money
          wx.redirectTo({
            url: `/package/pages/useCouponDetail/useCouponDetail?memberCardCode=${memberCardCode}&money=${money}&saleMode=${saleMode}&orgname=${orgname}&type=1`,
          })
        }
      }
    })
  },
  onLoad: function () {
    const that = this;

    that.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    const that = this;
    clearTimeout(timeOut)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})